package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.Base;
import com.fishery.base.entity.Germchit;
import com.fishery.base.entity.vo.FeedingRecordVo;
import com.fishery.base.entity.vo.LeaderPondVo;
import com.fishery.base.entity.vo.TraceCatchRecordVo;
import com.fishery.base.entity.vo.TracePondVo;
import com.fishery.base.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class TraceabilityServiceImpl implements TraceabilityService {

	@Autowired
	private CatchService catchService;

	@Autowired
	private PondService pondService;

	@Autowired
	private GermchitService germchitService;

	@Autowired
	private GermchitPondService germchitPondService;

	@Autowired
	private PondFeedingService pondFeedingService;

	@Autowired
	private BaseService baseService;

	@Autowired
	private ExecutorService threadPool;

	@Override
	public Map<String, Object> traceInformation(String productId, int current, int size) {
		Map<String, Object> resultMap = new ConcurrentHashMap<>(8);
		CountDownLatch countDownLatch = new CountDownLatch(4);
		// 通过 productId 溯源到 养殖基地的捕捞记录中的 pondId、germchitId、捕捞人名、farmingId
		TraceCatchRecordVo catchRecord = catchService.traceCatchRecordById(productId);
		resultMap.put("farmingId", catchRecord.getFarmingId());
		resultMap.put("catchTime", catchRecord.getGmtCreate());
		// 通过catchRecord中的pondId查询池塘信息
		threadPool.submit(() -> {
			TracePondVo pond = pondService.tracePondById(catchRecord.getPondId());
			resultMap.put("pondInfo", pond);
			// 通过pond中的baseId查询基地信息
			Base base = baseService.getById(pond.getBaseId());
			resultMap.put("baseInfo", base);
			// 通过pond中的baseId查询基地信息
			LeaderPondVo pondVo = pondService.getPondAmountAndArea(pond.getBaseId());
			resultMap.put("pondAmountAndArea", pondVo);
			countDownLatch.countDown();
		});
		// 通过 catchRecord中的germchitId 溯源到 种苗信息表可以溯源种苗的相关信息
		threadPool.submit(() -> {
			Germchit germchit = germchitService.traceGermchitById(catchRecord.getGermchitId());
			resultMap.put("germchitInfo", germchit);
			countDownLatch.countDown();
		});
		// 通过 catchRecord中的farmingId 溯源到 投苗时间
		threadPool.submit(() -> {
			Date seedingTime = germchitPondService.traceSeedingTimeById(catchRecord.getFarmingId());
			resultMap.put("seedingTime", seedingTime);
			countDownLatch.countDown();
		});
		// 通过 catchRecord中的farmingId 溯源到 喂养表中的喂养情况（包含supplyId，投入品名,饲养员名、饲养记录）
		threadPool.submit(() -> {
			Page<FeedingRecordVo> feedingRecords = pondFeedingService.traceFeedingByFarmingId(catchRecord.getFarmingId(),
					current, size, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(catchRecord.getGmtCreate()));
			resultMap.put("feedingRecords", feedingRecords);
			countDownLatch.countDown();
		});
		try {
			countDownLatch.await(3000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return resultMap;
	}
}
